import { reactive, computed, ref } from 'vue'
import { defineStore } from 'pinia'

export const useFriendStore = defineStore('friend', () => {
        let friends = ref([])
        const setFriend = (data) => {
            // if (data) {
            //     data.forEach(item => {
            //         friends.push(item);

            //     });
            // }

            // friends.push(data);
            // console.log('赋值数组')
            // console.log(data)

            friends.value = data
                // console.log('赋值完')
                // console.log(friends)
        }

        const friendDetail = reactive({
            id: '',
            name: '',
            img: '',
            signature: ''
        })
        const setFriendDetail = (data) => {
            friendDetail.id = data.id;
            friendDetail.name = data.name;
            friendDetail.img = data.img;
            friendDetail.signature = data.signature;
        }
        const removeFriend = (id) => {
            console.log('删除好友', friends.value)
            console.log(id)
            const index = friends.value.findIndex(item => {
                if (item.id + '' === id + '') {
                    return true;
                }
            })
            console.log('删除好友', index)
            if (index) {
                friends.value.splice(index, 1);
            }
            console.log('删除完好友', friends.value)

        }

        return { friends, setFriend, friendDetail, setFriendDetail, removeFriend }
    }

)